<template>
  <div class="app-container">
    <el-button type="primary" style="margin-bottom: 20px;" @click="openDialog(null)"><i class="el-icon-edit" />新建角色</el-button>
    <el-table
      :data="tableData"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="id"
      />
      <el-table-column
        prop="name"
        label="角色名称"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="openDialog(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="remove(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right;margin-top: 30px;">
      <el-pagination
        :page-sizes="[10, 20, 50, 100]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="currentPage"
        :hide-on-single-page="!total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog title="角色编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="名称" label-width="120px">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="分配菜单" label-width="120px">
          <el-cascader
            v-model="form.menus"
            placeholder="分配菜单"
            :options="list"
            :props="props"
            clearable
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRolesList, addOrEditRole, del } from '@/api/roles'
import { getMenus } from '@/api/menus'

export default {
  inject: ['reload'],
  data() {
    return {
      tableData: [],
      form: {
        id: '',
        name: '',
        menus: []
      },
      props: {
        value: 'id',
        label: 'name',
        children: 'children',
        multiple: true
      },
      list: [],
      dialogFormVisible: false,
      currentPage: 1,
      total: 0,
      size: 10
    }
  },
  created() {
    this.getList()
    this.getMenusList()
  },

  methods: {
    getList: function() {
      getRolesList(this.currentPage, this.size).then(res => {
        const { data } = res
        this.tableData = data.list
        this.total = data.total
      })
    },

    getMenusList() {
      getMenus().then(res => {
        const { data } = res
        this.list = data
      })
    },

    openDialog(data) {
      if (data === null) {
        this.form = {
          id: '',
          name: '',
          menus: []
        }
      } else {
        this.form = JSON.parse(JSON.stringify(data))
      }
      this.dialogFormVisible = true
    },

    handleSizeChange(val) {
      this.size = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    edit() {
      this.dialogFormVisible = false
      addOrEditRole(this.form).then(res => {
        this.getList()
      })
    },

    remove(data) {
      del(data.id).then(res => {
        this.getList()
      })
    }
  }
}
</script>
